<?php
/**
 * 促销页面
 *
 */
defined('InShopNC') or exit('Access Invalid!');
class promotionsControl extends MobileControl{
    public function __construct(){
        parent::__construct();
    }
    private  $promotions_group=array(PROMOTIONS_NEW=>'新品推荐',PROMOTIONS_BEST=>'畅销爆款',PROMOTIONS_SPECIAL=>'特价专区'/*,PROMOTIONS_GIVE=>'有买有赠'*/);//专区分组
    private $page_size = 10;
	public function promotionsOp(){
        $_GET['promotions_type_id']=empty($_GET['promotions_type_id'])?PROMOTIONS_MF:$_GET['promotions_type_id'];
     if(!empty($_GET['promotions_type_id'])){
         $sql=" AND p.promotions_type_id='{$_GET['promotions_type_id']}'";
         if($_GET['promotions_type_id']==PROMOTIONS_MF){
             $title='美发';
             $this->GetAdv('432','one_adv');
             $this->GetAdv('433','two_adv');
         }
         if($_GET['promotions_type_id']==PROMOTIONS_MR){
             $title='美容';
             $this->GetAdv('437','one_adv');
             $this->GetAdv('438','two_adv');
         }
     }


        $promotions=array();
     foreach($this->promotions_group as $k=>$v){
        if($k == PROMOTIONS_NEW){
            $promotions[$k]=db::queryAll("SELECT g.goods_sort,g.goods_id,g.store_id,g.goods_name,g.goods_store_price,g.goods_image,g.xianshi_flag,g.xianshi_discount FROM #__goods g LEFT JOIN #__store_class_goods as a ON g.goods_id = a.goods_id LEFT JOIN #__store_goods_class as b ON a.stc_id=b.stc_id WHERE g.store_id=".OFFICIAL_STORE_ID." and g.goods_show=1 and g.goods_state=0 and b.stc_type_id=".$_GET['promotions_type_id']." ORDER BY g.goods_id desc LIMIT 0,{$this->page_size}");
        }else{
            $promotions[$k]=db::queryAll("SELECT g.goods_sort,g.goods_id,g.store_id,g.goods_name,g.goods_store_price,g.goods_image,g.xianshi_flag,g.xianshi_discount FROM #__promotions p LEFT JOIN #__goods g ON g.goods_id=p.promotions_goods_id WHERE p.promotions_group_id='{$k}'".$sql." ORDER BY p.promotions_id desc LIMIT 0,{$this->page_size}");
        }
     }

     Tpl::output('promotions',$promotions);
     Tpl::output('html_title',$title.'促销专区——D1美财网');
     Tpl::output('promotions_group',$this->promotions_group);
     Tpl::showpage('promotions');
    }

    public function listOp(){

        $curPage=$_GET['curpage'];
        $offset = ($curPage-1)*$this->page_size;
        //  新品sql
        if(!empty($_GET['promotions_group_id']) && $_GET['promotions_group_id'] == PROMOTIONS_NEW){
            $sql="SELECT g.goods_sort,g.goods_id,g.store_id,g.goods_name,g.goods_store_price,g.goods_image FROM  #__goods g LEFT JOIN #__store_class_goods as a ON g.goods_id = a.goods_id LEFT JOIN #__store_goods_class as b ON a.stc_id=b.stc_id WHERE g.goods_state='0' and g.store_id=".OFFICIAL_STORE_ID." and g.goods_show= '1' and b.stc_type_id={$_GET['promotions_type_id']} ";
        
        }else{
            $sql="SELECT g.goods_sort,g.goods_id,g.store_id,g.goods_name,g.goods_store_price,g.goods_image FROM #__promotions p LEFT JOIN #__goods g ON g.goods_id=p.promotions_goods_id WHERE g.goods_state='0' and g.goods_show= '1' and p.promotions_type_id='{$_GET['promotions_type_id']}' and p.promotions_group_id='{$_GET['promotions_group_id']}'";
        }
        if(!empty($_GET['promotions_group_id']) && $_GET['promotions_group_id'] == PROMOTIONS_NEW){
            $order = 'g.goods_id desc';//设置排序;
        }else{
            $order ='p.promotions_id desc';//设置排序
        }
        $sql.=" ORDER BY $order limit $offset,{$this->page_size}" ;
        $goods_all=Db::queryAll($sql);
        //对商品图片处理
        foreach($goods_all as $k=>$v){
            $v['goods_image'] = thumb($v,'small');
            $goods_all[$k]=$v;
        }
        if($goods_all){
            echo json_encode(array('status'=>true,'goods_all'=>$goods_all,'is_login'=>$_SESSION['is_login']));exit;
        }else{
            echo json_encode(array('status'=>false));exit;
        }

    }
    /*获取广告*/
    private function GetAdv($ap_id,$name){
        $now_time = time();
        $sql = "SELECT * FROM #__adv WHERE ap_id={$ap_id} AND is_allow = '1' AND adv_start_date < $now_time AND adv_end_date > $now_time ORDER BY adv_id DESC";
        $adv = Db::queryRow($sql);
        if(is_array($adv)&&count($adv)>0){
            $adv['info']=unserialize($adv['adv_content']);
        }
        Tpl::output($name,$adv);
    }
}